package com.data.listenner

import com.data.bean.Account

import javax.servlet.http.HttpSession
import javax.servlet.http.HttpSessionEvent
import javax.servlet.http.HttpSessionListener
import java.util.HashMap

/**
 * 单一地点登陆监听器
 *
 * @author:HingLo
 * @create 2017-10-12 17:03
 */
class SessionLoginListener : HttpSessionListener {

    override fun sessionCreated(httpSessionEvent: HttpSessionEvent) {

    }

    /**
     * 销毁session前的执行操作
     *
     * @param httpSessionEvent
     */
    override fun sessionDestroyed(httpSessionEvent: HttpSessionEvent) {
        val session = httpSessionEvent.session
        delSession(session)
    }

    companion object {
        var sessionMap = HashMap<String, HttpSession>()

        @Synchronized
        fun delSession(session: HttpSession?) {
            if (session != null) {
                // 删除单一登录中记录的变量
                if (session.getAttribute("Account") != null) {
                    val account = session.getAttribute("Account") as Account
                    SessionLoginListener.sessionMap.remove(account.id)
                }
            }
        }
    }
}
